<?php
	init_set('error_reporting',E_ERROR);
	include_once "inc_function.php";
?>
<script type="text/javascript" src="<?= 'http://'.JS_PATH;?>employee/fn_emp.js"></script>
<body>
<script type="text/javascript">
	function get_employee(id,c_name){
    	$("#superior_name").val(c_name);
    	$("#superior_id").val(id);
	}
	$(document).ready(function(){
		//date picker
		$( "#join_date" ).datepicker({
		showOn: "button",
		buttonImage: "<?= 'http://'.IMAGE_PATH?>calendar.gif",
		buttonImageOnly: true
		});

		$("#emp_name").autocomplete("auto_employee.php", {
        	width: 150
        });
			         		
	});
</script>
<?php
   $curs_job = oci_new_cursor($con);
   $stmt = oci_parse($con,"begin pkg_master.sp_get_job_list(1,:data);end;");
   oci_bind_by_name($stmt,":data",&$curs_job,-1,OCI_B_CURSOR);
   oci_execute($stmt,OCI_DEFAULT);
   oci_execute($curs_job,OCI_DEFAULT);
   oci_free_statement($stmt);

   $curs_company = oci_new_cursor($con);
   $stmt = oci_parse($con,"begin pkg_master.sp_get_company_list(1,:data);end;");
   oci_bind_by_name($stmt,":data",&$curs_company,-1,OCI_B_CURSOR);
   oci_execute($stmt,OCI_DEFAULT);
   oci_execute($curs_company,OCI_DEFAULT);
   oci_free_statement($stmt);

   $curs_division = oci_new_cursor($con);
   $stmt = oci_parse($con,"begin pkg_master.sp_get_division_list(1,:data);end;");
   oci_bind_by_name($stmt,":data",&$curs_division,-1,OCI_B_CURSOR);
   oci_execute($stmt,OCI_DEFAULT);
   oci_execute($curs_division,OCI_DEFAULT);
   oci_free_statement($stmt);
     $status =  ($_POST['emp_active'] == 'on' && isset($_POST['emp_active'])) ? 1 : 0;

   switch($_POST['action']){
      case 'add' :
         $stmt = oci_parse($con,"begin pkg_master.sp_insert_m_employee(:s_employee_name,:s_job_title_id,:s_main_company_id,:s_create_id,:s_update_id,:s_join_date,:s_superior_id,:s_division_id,:s_status);end;");
         oci_bind_by_name($stmt,":s_employee_name",$_POST['emp_name']);
         oci_bind_by_name($stmt,":s_job_title_id",$_POST['job_title']);
         oci_bind_by_name($stmt,":s_main_company_id",$_POST['company']);
         oci_bind_by_name($stmt,":s_create_id",$_POST['person']);
         oci_bind_by_name($stmt,":s_update_id",$_POST['person']);
         oci_bind_by_name($stmt,":s_join_date",$_POST['join_date']);
         oci_bind_by_name($stmt,":s_superior_id",$_POST['superior_id']);
         oci_bind_by_name($stmt,":s_division_id",$_POST['division']);
         oci_bind_by_name($stmt,":s_status",$status);
         oci_execute($stmt,OCI_DEFAULT);

         	$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
         
		 header("Location: list_employee.php");
      break;
      case 'update' :
         $stmt = oci_parse($con,"begin pkg_master.sp_update_m_employee(:s_employee_id,:s_employee_name,:s_job_title_id,:s_main_company_id,:s_update_id,:s_join_date,:s_superior_id,:s_division_id,:s_status);end;");
         oci_bind_by_name($stmt,":s_employee_id",$_POST['emp_id']);
         oci_bind_by_name($stmt,":s_employee_name",$_POST['emp_name']);
         oci_bind_by_name($stmt,":s_job_title_id",$_POST['job_title']);
         oci_bind_by_name($stmt,":s_main_company_id",$_POST['company']);
         oci_bind_by_name($stmt,":s_update_id",$_POST['person']);
         oci_bind_by_name($stmt,":s_join_date",$_POST['join_date']);
         oci_bind_by_name($stmt,":s_superior_id",$_POST['superior_id']);
         oci_bind_by_name($stmt,":s_division_id",$_POST['division']);
         oci_bind_by_name($stmt,":s_status",$status);
         oci_execute($stmt,OCI_DEFAULT);

         	$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
         
         header("Location: list_employee.php");
      break;
      default :
      break;
   }

   if($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])){
		    $curs = oci_new_cursor($con);
		   	$stmt = OCIParse($con,"begin pkg_master.sp_get_employee_by_id(:employee_id,:c_employee_list); end;");
		   	OCIBindByName($stmt,":employee_id",$_REQUEST['update']);
		   	OCIBindByName($stmt,":c_employee_list",&$curs,-1,OCI_B_CURSOR);
		   	ociexecute($stmt,OCI_DEFAULT);
		   	ociexecute($curs,OCI_DEFAULT);

   	while (OCIFetchInto($curs,&$o_employee_list))
  	   {
       	$emp_id   = $o_employee_list[0];
       	$emp_name = $o_employee_list[1];
       	$job = $o_employee_list[2];
       	$company = $o_employee_list[4];
       	$join_date = date('m/d/Y',strtotime($o_employee_list[10]));
         $superior_id = $o_employee_list[11];
         $superior_name = $o_employee_list[12];      
         $division = $o_employee_list[13];
         $status = $o_employee_list[15];
  	   }
  	   oci_free_statement($stmt);
  	   oci_free_statement($curs);

      $checked = ($status) ? 'checked=checked' : '';
   }

   $form = ($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])) ? 'update' : 'add' ;

   function chekedValidation($content,$validation){
      if($content == $validation){
         return 'selected=selected';
      }
   }
?>
<form name='frm_emp_register' action method='post' onsubmit='return submitValidation()'>
<fieldset class="frm_registered">
   <legend> Form Registered Employee</legend>
   <input type='hidden' name='emp_id' value="<?= $emp_id ?>" />
   <input type='hidden' name='action' value="<?= $form ?>" />
   <input type='hidden' name='person' id='person' value="<?= $_SESSION['employee_id']?>" />
   <input type='hidden' name='superior_id' value='<?= $superior_id ?>' id="superior_id"/>
   
   <div class='formInput'>
   <div>
   	<label> Employee Name <span class='red'>*</span></label> : <input type='text' name='emp_name' class='textbox' id='emp_name' value='<?= $emp_name;?>' onBlur="return cekValidation(this.value,this.id);"/>
      <sup class='red error_emp_name hidden'> * Required </sup>
   	<input type='checkbox' name='emp_active' id='emp_active' style='width:30px;' <?= $checked;?>/>Active
   </div>

   <div class="hidden">
   	<label> Login Name <span class='red'>*</span></label> : <input type='text' name='login_name' class='textbox' id='login_name' value='' onBlur="return cekValidation(this.value,this.id);"/>
      <a href='<?= AJAX_PATH?>list_login.php?keepThis=true&TB_iframe=true&height=400&width=500' class='thickbox'><img src='http://<?= IMAGE_PATH?>search.png' style='cursor:pointer;'/></a>
      <sup class='red error_login_name hidden'> * Required </sup>
   </div>

   <div class="hidden">
   	<label> Login Code <span class='red'>*</span></label> : <input type='text' name='login_code' class='textbox' id='login_code' value='' onBlur="return cekValidation(this.value,this.id);"/>
      <sup class='red error_login_code hidden'> * Required </sup>
   </div>

   <div class="hidden">
   	<label> Password <span class='red'>*</span></label> : <input type='text' name='password' class='textbox' id='password' value='' onBlur="return cekValidation(this.value,this.id);"/>
      <sup class='red error_password hidden'> * Required </sup>
   </div>

   <div class="hidden">
   	<label> Email <span class='red'>*</span></label> : <input type='text' name='email' class='textbox' id='email' value='' onBlur="return cekValidation(this.value,this.id);"/>
   	<sup class='red error_email hidden'> * Required </sup>
   </div>

   <div>
   	<label> Job Title <span class='red'>*</span></label> :
   	  <select class="ck_box" id="job_title" name="job_title" onChange="return cekValidation(this.value,this.id);">
            <option value='0'> --- PILIH --- </option>
            <?php while (OCIFetchInto($curs_job,&$r_job_list)) { ?>
            <option value="<?= $r_job_list[0]?>" <?= chekedValidation($r_job_list[0],$job);?>> <?= $r_job_list[1]?> </option>
            <?php } ?>
   	  </select>
   	  <sup class='red error_job_title hidden'> * Required </sup>
   </div>

   <div>
   	<label> Company <span class='red'>*</span></label> :
   	  <select class="ck_box" id="company" name="company" onChange="return cekValidation(this.value,this.id);">
            <option value='0'> --- PILIH --- </option>
            <?php while (OCIFetchInto($curs_company,&$r_company_list)) { ?>
            <option value="<?= $r_company_list[0]?>" <?= chekedValidation($r_company_list[0],$company);?>> <?= $r_company_list[2]?> </option>
            <?php } ?>
   	  </select>
   	  <sup class='red error_company hidden'> * Required </sup>
   </div>

   <div>
   	<label> Superior <span class='red'>*</span></label> : <input type='text' name='superior_name' class='textbox' id='superior_name' value='<?= $superior_name ?>' onBlur="return cekValidation(this.value,this.id);"/>
      <a href='<?= AJAX_PATH?>list_superior.php?auto=true&keepThis=true&TB_iframe=true&height=400&width=500' class='thickbox'><img src='http://<?= IMAGE_PATH?>search.png' style='cursor:pointer;'/></a>
      <sup class='red error_superior_name hidden'> * Required </sup>
   </div>

   <div>
   	<label> Division <span class='red'>*</span></label> :
   	  <select class="ck_box" id="division" name="division" onChange="return cekValidation(this.value,this.id);">
            <option value='0'> --- PILIH --- </option>
            <?php while (OCIFetchInto($curs_division,&$r_division_list)) { ?>
            <option value="<?= $r_division_list[0]?>" <?= chekedValidation($r_division_list[0],$division);?>> <?= $r_division_list[2]?> </option>
            <?php } ?>
   	  </select>
      <sup class='red error_division hidden'> * Required </sup>
   </div>

   <div>
   	<label> Join Date <span class='red'>*</span></label> : <input type='date' name='join_date' class='datepicker' id='join_date' value='<?= $join_date;?>' onBlur="return cekValidation(this.value,this.id);"/>
      <sup class='red error_join_date hidden'> * Required </sup>
   </div>
   </div>
   <div class="mybutton"> <button type="submit" class="button"> Save </button> <button type="reset" class="button"> Reset </button></div>

</fieldset>
</form>

</body>
<?php
   oci_free_statement($curs_job);
   oci_free_statement($curs_company);
   oci_free_statement($curs_division);
?>